
package controllers;

import dao.ReportDAO;
import entities.Report;
import java.util.ArrayList;
import java.util.List;
import vo.ReportVO;

public final class ReportHistoryController {
    
    private final static String persistenceUnit="satacreditoclientePU";
    
    public static List<ReportVO> getReportHistory(){
        List<ReportVO> reportsVO=new ArrayList<>();
        try {
            List<Report> reports=new ReportDAO(persistenceUnit).getReports();
            for (Report report : reports)
                reportsVO.add(reportToVO(report));
        } catch (Exception e) {
            return null;
        }
        return reportsVO;
    }
    
    public static boolean saveReport(ReportVO reportVO){
        try {
            new ReportDAO(persistenceUnit).create(VOToReport(reportVO));
        } catch (Exception e) {
            return false;
        }
        return true;
    }
    
    private static ReportVO reportToVO(Report report){
        ReportVO reportVO=new ReportVO();
        reportVO.setReportId(report.getReportId());
        reportVO.setType(report.getType());
        reportVO.setBranch(report.getBranch());
        reportVO.setFranchise(report.getFranchise());
        reportVO.setAmount(report.getAmount());
        reportVO.setStartDate(report.getStartDate());
        reportVO.setEndDate(report.getEndDate());
        reportVO.setIssueDate(report.getIssueDate());
        reportVO.setLocation(report.getLocation());
        return reportVO;
    }
    
    private static Report VOToReport(ReportVO reportVO){
        Report report=new Report();
        report.setReportId(reportVO.getReportId());
        report.setType(reportVO.getType());
        report.setBranch(reportVO.getBranch());
        report.setFranchise(reportVO.getFranchise());
        report.setAmount(reportVO.getAmount());
        report.setStartDate(reportVO.getStartDate());
        report.setEndDate(reportVO.getEndDate());
        report.setIssueDate(reportVO.getIssueDate());
        report.setLocation(reportVO.getLocation());
        return report;
    }
    
}